import { Button, Label, native, UITransform } from "cc";
import BaseApp from "../base/BaseApp";
import BaseView from "../base/BaseView";
import GameConfig from "../base/GameConfig";
import TestView2 from "../views/TestView2";
import { sys } from "cc";
import TableView, { cellAgent, tvCell } from "../base/compoment/TableView";
import i18n from "../base/i18n/LanguageData";

export default class TestView extends BaseView implements cellAgent {
	get url():string{return "Test:layout"}

	private _tbview: TableView;

	private _celldata:any =[];

	constructor(data:string){
		super();
		console.log(data);
		if(sys.isNative){
			native.jsbBridgeWrapper.dispatchEventToNative("text","{val:1}");
			native.jsbBridgeWrapper.addNativeEventListener("changetest",(arg)=>{
				console.log("xxx",arg)
			})
		}

	}
	getSize(index: number, data?: any): number {
		return index%5==0? 100:50;
	}
	initCell(cell: tvCell, data?: any, tv?: TableView){
		cell.getChildByName("label").getComponent(Label).string = data.desc;
		cell.getChildByName("bg").getComponent(UITransform).height = cell.tvIndex %5==0? 100:50;
	}
	onLoad(){
		for (let i = 0; i < 100; i++){
			this._celldata.push({
				id: i,
				desc: "desc" + i,
			});
		}

		const button = this.node.getChildByPath("content/button1");

		button.on("click", this.onBtnClickEvent, this);
		this.node.getChildByPath("content/button3").on("click", this.onBtnClickEvent2, this);
		this.node.getChildByPath("content/button2").on("click", this.onBtnClickEvent2, this);
		this.node.getChildByPath("content/button5").on("click", this.onBtnClickEvent2, this);

		let node = this.node.getChildByPath("content/scrollview");

		this._tbview = node.getComponent(TableView);
		this._tbview.init(this, this._celldata);
		node.on("scroll-to-bottom",()=>{
			console.log("scroll-to-bottom");
		});

		BaseApp.instance().eventTarget.on("foobar",this.onEvent,this); 
		node = this.node.getChildByPath("content/labelevent");
		BaseApp.instance().appComponent.scheduleOnce(()=>{  // 1s后节点可能被销毁了
			if(node.isValid) node.getComponent(Label).string = "ON LOAD"; }, 1.0);
	}

	onClose():void{
		BaseApp.instance().eventTarget.off("foobar",this.onEvent,this);
	}

	onBtnClickEvent2(btn:Button):void{
		// this.scroll.init(50,2);
		if(btn.node.name == "button3"){
			this._celldata[2].desc = "update";
			this._celldata[5].desc = "update";
			this._celldata.splice(10,1);
			// this.scroll.reload();
			this._tbview.refresh(true);
			this._tbview.scheduleOnce(()=>{
				this._tbview.scrollToIndex(10,1);
			},1);
            
		}else if(btn.node.name == "button5"){
			this.closeSubView(TestView2);
		}else if(btn.node.name == "itembtn"){
			const tvcell = btn.node.parent as tvCell;
			console.log(tvcell.tvIndex);
		}else{
			this.openSubView(TestView2);
		}
	}

	onEvent(message):void{
		console.log("TestView onEvent");
		const node = this.node.getChildByPath("content/labelevent");

		node.getComponent(Label).string = message;
	}

	onBtnClickEvent(event):void{
		BaseApp.instance().closeViewByName(this.node.name);
		// let tb = GameConfig.Instance().getTable("activity_type");
		// console.log(tb[1].id);
		const tb = GameConfig.instance().getTableValueByKey("skin","num",0)

		console.log(tb.num);
		i18n.init("en");
	}
}